package com.kateba.android.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class TeksoftAdView extends RelativeLayout
{
  private int clrBackground = Color.argb(180, 200, 200, 200);
  private int clrText = -16777216;
  private Context mContext;
  private int m_LastAdShown = 0;
  private int m_SecRotateAds = -1;
  private int m_SecToLive = -1;
  private int m_ServiceIndex = 0;
  private List<String> m_adsList = new ArrayList();
  private boolean m_bIsDismissed = false;
  private boolean m_bShowStats = false;
  private ImageView m_ivLogo;
  private TextView m_tvCount;
  private TextView m_tvLogo;
  private TextView m_tvStats;
  private WebView m_web;
  private Handler serviceHandler = null;

  public TeksoftAdView(Context paramContext)
  {
    super(paramContext);
    this.mContext = paramContext;
    setBackgroundColor(this.clrBackground);
    setPadding(2, 2, 2, 2);
    setLayoutParams(new RelativeLayout.LayoutParams(-2, -2));
    setGravity(16);
    LinearLayout localLinearLayout1 = new LinearLayout(paramContext);
    localLinearLayout1.setId(1);
    localLinearLayout1.setOrientation(1);
    localLinearLayout1.setGravity(16);
    RelativeLayout.LayoutParams localLayoutParams1 = new RelativeLayout.LayoutParams(-2, -2);
    localLayoutParams1.addRule(9);
    localLayoutParams1.addRule(15);
    addView(localLinearLayout1, localLayoutParams1);
    this.m_ivLogo = new ImageView(paramContext);
    this.m_ivLogo.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramView)
      {
        TeksoftAdView.this.CloseAdAnimated();
      }
    });
    localLinearLayout1.addView(this.m_ivLogo);
    this.m_tvLogo = new TextView(paramContext);
    this.m_tvLogo.setTextColor(this.clrText);
    this.m_tvLogo.setTextSize(2, 9.0F);
    localLinearLayout1.addView(this.m_tvLogo);
    LinearLayout localLinearLayout2 = new LinearLayout(paramContext);
    localLinearLayout2.setId(3);
    localLinearLayout2.setOrientation(1);
    localLinearLayout2.setGravity(16);
    RelativeLayout.LayoutParams localLayoutParams2 = new RelativeLayout.LayoutParams(-2, -2);
    localLayoutParams2.addRule(11);
    localLayoutParams2.addRule(15);
    addView(localLinearLayout2, localLayoutParams2);
    this.m_tvStats = new TextView(paramContext);
    this.m_tvStats.setText(" ");
    this.m_tvStats.setTextColor(this.clrText);
    this.m_tvStats.setTextSize(2, 9.0F);
    localLinearLayout2.addView(this.m_tvStats);
    this.m_tvCount = new TextView(paramContext);
    this.m_tvCount.setText(" ");
    this.m_tvCount.setTextColor(this.clrText);
    this.m_tvCount.setTextSize(2, 10.0F);
    localLinearLayout2.addView(this.m_tvCount);
    this.m_web = new WebView(paramContext);
    this.m_web.setBackgroundColor(0);
    RelativeLayout.LayoutParams localLayoutParams3 = new RelativeLayout.LayoutParams(-1, -2);
    localLayoutParams3.addRule(15);
    localLayoutParams3.addRule(1, localLinearLayout1.getId());
    localLayoutParams3.addRule(0, localLinearLayout2.getId());
    addView(this.m_web, localLayoutParams3);
    this.serviceHandler = new Handler();
    this.serviceHandler.postDelayed(new RunTask(), 1000L);
  }

  public TeksoftAdView(Context paramContext, AttributeSet paramAttributeSet)
  {
    super(paramContext, paramAttributeSet);
    this.mContext = paramContext;
  }

  private void CloseAd()
  {
    RelativeLayout localRelativeLayout = (RelativeLayout)getParent();
    if (localRelativeLayout != null)
    {
      this.m_bIsDismissed = true;
      localRelativeLayout.removeView(this);
    }
  }

  private void CloseAdAnimated()
  {
    AnimationSet localAnimationSet = new AnimationSet(true);
    ScaleAnimation localScaleAnimation = new ScaleAnimation(1.0F, -10.0F, 1.0F, -10.0F);
    localScaleAnimation.setDuration(1000L);
    localAnimationSet.addAnimation(localScaleAnimation);
    localAnimationSet.setAnimationListener(new Animation.AnimationListener()
    {
      public void onAnimationEnd(Animation paramAnimation)
      {
        TeksoftAdView.this.CloseAd();
      }

      public void onAnimationRepeat(Animation paramAnimation)
      {
      }

      public void onAnimationStart(Animation paramAnimation)
      {
      }
    });
    startAnimation(localAnimationSet);
  }

  public void AddAd(String paramString, boolean paramBoolean)
  {
    if (paramString != null)
    {
      this.m_adsList.add(paramString);
      if (paramBoolean)
        NextAd(false);
    }
  }

  public void AddOnlineAd(String paramString, boolean paramBoolean)
  {
    new Thread(paramString)
    {
      private String val$link;
	private boolean val$refresh;

	public void run()
      {
        String str = Utils.DownloadStringFromWeb(this.val$link);
        TeksoftAdView.this.AddAd(str, this.val$refresh);
      }
    }
    .start();
  }

  public boolean IsDismissed()
  {
    return this.m_bIsDismissed;
  }

  public void NextAd(boolean paramBoolean)
  {
    if (this.m_adsList.size() > 0)
    {
      int i = 1;
      if (paramBoolean)
        i = new Random().nextInt(10);
      this.m_LastAdShown = ((i + this.m_LastAdShown) % this.m_adsList.size());
      this.m_web.loadDataWithBaseURL("http://", (String)this.m_adsList.get(this.m_LastAdShown), "text/html", "utf-8", "http://www.teksoftco.com");
      this.m_web.postInvalidate();
      if (this.m_bShowStats)
        this.m_tvStats.post(new Runnable()
        {
          public void run()
          {
            TeksoftAdView.this.m_tvStats.setText(1 + TeksoftAdView.this.m_LastAdShown + "/" + TeksoftAdView.this.m_adsList.size());
          }
        });
    }
  }

  public void SetBackground(int paramInt)
  {
    setBackgroundColor(paramInt);
  }

//  public void SetLogo(int paramInt, String paramString)
//  {
//    this.m_ivLogo.post(new Runnable(paramInt)
//    {
//      public void run()
//      {
//        TeksoftAdView.this.m_ivLogo.setImageResource(this.val$imgRes);
//      }
//    });
//    this.m_tvLogo.post(new Runnable(paramString)
//    {
//      public void run()
//      {
//        TeksoftAdView.this.m_tvLogo.setText(this.val$text);
//      }
//    });
//  }

  public void SetRotateAd(int paramInt)
  {
    this.m_SecRotateAds = paramInt;
  }

  public void SetSecToLive(int paramInt)
  {
    this.m_SecToLive = paramInt;
  }

  public void SetStats(boolean paramBoolean)
  {
    this.m_bShowStats = paramBoolean;
    if (!this.m_bShowStats)
      this.m_tvStats.post(new Runnable()
      {
        public void run()
        {
          TeksoftAdView.this.m_tvStats.setText("");
        }
      });
  }

  class RunTask
    implements Runnable
  {
    RunTask()
    {
    }

    public void run()
    {
      TeksoftAdView.this.m_ServiceIndex = ((1 + TeksoftAdView.this.m_ServiceIndex) % 3600);
      if ((TeksoftAdView.this.m_SecRotateAds > 0) && (TeksoftAdView.this.m_ServiceIndex % TeksoftAdView.this.m_SecRotateAds == 0))
        TeksoftAdView.this.NextAd(false);
      if (TeksoftAdView.this.m_SecToLive > 0)
      {
        TeksoftAdView localTeksoftAdView = TeksoftAdView.this;
        localTeksoftAdView.m_SecToLive = (-1 + localTeksoftAdView.m_SecToLive);
        if (TeksoftAdView.this.m_SecToLive != 0)
//          break label115;
        TeksoftAdView.this.CloseAdAnimated();
      }
      while (true)
      {
        TeksoftAdView.this.serviceHandler.postDelayed(this, 1000L);
//        return;
//        label115:
        	TeksoftAdView.this.m_tvCount.post(new Runnable()
        {
          public void run()
          {
            TeksoftAdView.this.m_tvCount.setText(TeksoftAdView.this.m_SecToLive);
          }
        });
      }
    }
  }
}